<template>
	<view class="test-view">
		<view class="title">
			<view>
				<text>{{form.name}}</text>
				<text>得分：{{answer.score}}/{{form.score}}</text>
			</view>
			<view>
				<text></text>
				<text>耗时：{{formatSeconds(answer.doTime)}}</text>
			</view>
		</view>
		<answerNode @goAnswerArc="goAnswerArc" :answerItem="answer.answerItems" :currentItemOrder="currentItemOrder"></answerNode>
		<!-- 题目 -->
		<scroll-view class="subjects" :show-scrollbar="false" scroll-y="true">
			<uni-forms ref="form" :model="form">
				<!--题目标题-->
				<uni-forms-item label="" name="">
					<answerSubject @answerPreAndNext="goAnswerArc"  :currentTitleItem="currentTitleItem" :currentQuestionItem="currentQuestionItem"
					:answer="answer.answerItems[currentItemOrder]"></answerSubject>
				</uni-forms-item>
			</uni-forms>
			
		</scroll-view>
	</view>
</template>

<script>
	import answerNode from "@/components/answerNode/answerNode.vue"
	import answerSubject from "@/components/answerSubject/answerSubject.vue"

	export default {
		
		data() {
			return {
				//存放试卷内容
				form: {},
				//存放用户答题内容
				answer: {
					id: null,
					score: 0,
					doTime: 0,
					answerItems:[],
					doRight: false
				},
				currentItemOrder: 0,
				currentQuestionItem: {},
				// currentAnswerItem: {},
				currentTitleItem: {}
			}
		},
		onLoad:function(options){
			//获取token
			let token = uni.getStorageSync("token");
			console.log(!token);
			if( !token){
				// uni.showToast({
				// 	title: "请先登录！！"
				// })
				uni.redirectTo ({
					url: "/pages/login/login"
				});
			}
			console.log(options.id);
			
			//要查询的成绩id
			let id = options.id;
			if( id && parseInt(id) !== 0){
				this.getExamPaperAnswer(id);
			}
		},
		components:{
			answerNode
		},
		methods: {
			 formatSeconds (theTime) {
				var secondTime = parseInt(theTime); // 秒
				var minuteTime = 0; // 分
				var hourTime = 0; // 小时
				if (secondTime >= 60) {
					minuteTime = parseInt(secondTime / 60);
					secondTime = parseInt(secondTime % 60);
					if (minuteTime >= 60) {
						hourTime = parseInt(minuteTime / 60);
						minuteTime = parseInt(minuteTime % 60);
					}
				}
				var result ="" +(parseInt(secondTime) < 10? "0" + parseInt(secondTime): parseInt(secondTime));
  
				// if (minuteTime > 0) {
					result ="" + (parseInt(minuteTime) < 10? "0" + parseInt(minuteTime) : parseInt(minuteTime)) + ":" + result;
				// }
				// if (hourTime > 0) {
					result ="" + (parseInt(hourTime) < 10 ? "0" + parseInt(hourTime): parseInt(hourTime)) +":" + result;
				// }
				return result;
			},
			//节点切换
			goAnswerArc(selector){
				if(selector >= this.answer.answerItems.length){
					uni.showToast({
						title: "这是最后一题了"
					})
					return false;
				}
				//切换显示题目
				this.currentItemOrder = selector;
				
				let titleItemArray = this.form.titleItems;
				for( let tIndex in titleItemArray){
					let questionArray = titleItemArray[tIndex].questionItems;
					for(let qIndex in questionArray){
						let question = questionArray[qIndex]
						if(question.itemOrder ==  selector){
							this.currentQuestionItem=question;
							this.currentTitleItem = titleItemArray[tIndex];
							return;
						}
					}
				}
				
			},
			//根据id查询 答题情况
			async getExamPaperAnswer(id){
				const resp = await this.$myRequest({
					url : "/api/student/exampaper/answer/read/"+id
				});
				if(resp.data.code == 200){
					this.form = resp.data.obj.paper;
					this.answer = resp.data.obj.answer;
					this.currentItemOrder = resp.data.obj.answer.answerItems[0].itemOrder;
					
					this.currentTitleItem = this.form.titleItems[0];
					this.currentQuestionItem = this.currentTitleItem.questionItems[0];
					// this.currentAnswerItem = resp.data.obj.answer.answerItems[0];
				}
			}
		}
	}
</script>

<style lang="scss">
.test-view{
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-items: center;
	width:100%;
	box-sizing: border-box;
	margin-bottom: 20rpx;
	padding-top: 25rpx;
	.title{
		width: 92%;
		view{
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			color:#212121;
			margin-top: 10rpx;
		}
	}
	.subjects{
		width: 92%;
		height: 740rpx;
		border-radius: 20rpx;
		background: #fff;
		margin-top: 40rpx;
		box-sizing: border-box;
		border: 2rpx solid var(--themeColor);
		// padding: 0 20rpx;
		position: relative;
		
	}
}
</style>
